#include "compile_run.hpp"
#include "../comm/httplib.h"
#include "../comm/util.hpp"
#include "hehe.hpp"
#include <fstream>
using namespace std;

using namespace ns_compile_and_run;

using namespace httplib;

void Usage(string proc)
{
    cerr << "Usage: " << "\n\t" << proc << " <port>" << endl;
}

// 编译服务随时可能被多个人请求，必须保证传递上来的code，形成源文件名称的时候，要具有唯一性
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        return -1;
    }

    // 提供的编译服务，打包形成一个网络服务
    // cpp-httplib

    Server svr;

    svr.Get("/hello", [](const Request &req, Response &resp)
            { resp.set_content("hello world,hello 你好", "text/plain;charset=utf-8"); });

    svr.Post("/compile_and_run", [](const Request &req, Response &resp)
             {
       //用户请求的服务正文是我们想要的json string
       string in_json=req.body; 
       string out_json;
       if(!in_json.empty())
       {
            CompileAndRun::Start(in_json,&out_json);
            resp.set_content(out_json,"application/json;charset=utf-8");
       } });

    svr.Get("/hehe", [](const Request &req, Response &resp)
            {
        string out_json;
        handle_hehe(&out_json); 
        resp.set_content(out_json,"application/json;charset=utf-8");
        });
    svr.listen("0.0.0.0", atoi(argv[1])); // 启动http服务

    return 0;
}
